<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<c:import url="/includes/header.jsp"></c:import>
<c:import url="/includes/modal.jsp"></c:import>
</head>
<body>
	<div class="container" id="pager2">
		<form id="listForm" method="post"
			action="${pageContext.request.contextPath}/admin/setting/getAdmin.do">
			<div class="row">
				<div class="col-md-4" style="padding: 0; margin-bottom: 5px;">
					<a class="btn btn-primary"
						href="${pageContext.request.contextPath}/admin/setting/addUser.do">+
						新建管理员</a>
				</div>
				<div class="col-md-4"></div>
			</div>
			<div class="row">
				<table class="tab table table-striped table-bordered">
					<tr>
						<th>管理员</th>
						<th>邮箱</th>
						<th>创建时间</th>
						<th>状态</th>
						<th width="25%">操作</th>
					</tr>
					<c:forEach items="${admins}" var="a">
						<tr>
							<td>${a.username}</td>
							<td>${a.email}</td>
							<td><fmt:formatDate value="${a.createAt}"
									pattern="yyyy-MM-dd HH:mm:ss" /></td>
							<td><c:if test="${a.status == 0}">
									<button type="button" class="btn btn-success btn-sm disabled"
										data-status="0" data-id="${a.id}">正常</button>
								</c:if> <c:if test="${a.status == 1}">
									<button type="button" class="btn btn-warning btn-sm disabled"
										data-status="0" data-id="${a.id}">禁止</button>
								</c:if></td>

							<td><a class="btn btn-default btn-sm btn-edit" data-id="${a.id}">编辑</a>&nbsp;&nbsp;&nbsp;
								<a class="btn btn-default btn-sm btn-del" data-id="${a.id}">删除</a>


								<!--  
							<button type="button"
								class="btn btn-info btn-sm active btn-modify" data-status="1"
								data-href="${pageContext.request.contextPath}/admin/setting/enterPwd.do"
								data-id="${a.id}" data-toggle="modal">修改密码</button></td>
								-->
						</tr>
					</c:forEach>
				</table>
				<c:import url="/includes/page.jsp"></c:import>
			</div>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {

		$(".table").find("button[data-toggle='modal']").click(function() {

			var id = $(this).attr('data-id');
			var url = $(this).attr('data-href');
			url += '?id=' + id + '&t=' + Math.random(1000);
			$('#myModal').modal({
				remote : url
			})

		});

		//删除 
		$(".btn-del")
				.click(
						function() {
							var flag = confirm("确定删除？");
							var id = $(this).attr("data-id");
							var url = "${pageContext.request.contextPath}/admin/setting/delAdmin.do?id="
									+ id;
							if (flag == true) {
								var result = 0;
								$.ajax({
									type : "post",
									dataType : "json",
									url : url,
									async : false,
									success : function(data) {
										if (data.code == "0") {
											bootbox.alert("删除成功!");
											result = 1;
										} else {
											bootbox.alert("网络不稳定,运行失败");
										}
									},
									error : function() {
										bootbox.alert("网络不稳定,运行失败");
									}
								});
								if (result == 1) {
									$(this).parent().parent().hide(1000);
								}
							}
						});

		//编辑
		$(".btn-edit")
				.click(
						function() {
							var id = $(this).attr("data-id");
							var url = "${pageContext.request.contextPath}/admin/setting/editUser.do?id="
									+ id;
							window.location = url;
						});

		//禁止/解禁
		$(".btn-lock")
				.click(
						function() {
							var status = $(this).attr("data-status");
							var id = $(this).attr("data-id");
							var url = "${pageContext.request.contextPath}/admin/setting/lockUser.do";
							var result = false;
							var btnObject = this;
							if (status == 1) {
								result = confirm("禁止登录后用户将无法登录,确定?");
							} else {
								result = confirm("恢复正常登录,确定?");
							}
							if (result == true) {
								$.ajax({
									url : url,
									type : "post",
									dataType : "json",
									data : {
										"id" : id,
										"status" : status
									},
									async : true,
									success : function(data) {
										if (data.code == "0") {
											bootbox.alert("操作成功!");
											chageBtn(btnObject, data.data);

										} else {
											bootbox.alert("网络不稳定,操作失败");
										}
									},
									error : function() {
										bootbox.alert("网络不稳定,操作失败");
									}
								});
							}
						});

		var chageBtn = function(btnObject, status) {
			if (status == 0) {
				$(btnObject).attr("data-status", "1");
				$(btnObject).text("禁止登录");
				$(btnObject).attr("class", "btn btn-default btn-sm btn-lock");
				var firstBtn = $(btnObject).parent().parent().find(
						"button:eq(0)");
				$(firstBtn).text("正常");
				$(firstBtn).attr("class", "btn btn-success btn-sm disabled");
			} else {
				$(btnObject).attr("data-status", "0");
				$(btnObject).text("解禁登录");
				$(btnObject).attr("class", "btn btn-default btn-sm btn-lock");
				var firstBtn = $(btnObject).parent().parent().find(
						"button:eq(0)");
				$(firstBtn).text("禁止");
				$(firstBtn).attr("class", "btn btn-warning btn-sm disabled");

			}

		}

	});
</script>
